package com.cloudpivot.receivales.contract.util;

import lombok.extern.slf4j.Slf4j;

import java.net.URI;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;
import java.util.UUID;

/**
 * 常用工具
 */
@Slf4j
public class StrUtil {

    /**
     * 计算可用 合同编号
     * @param _cid
     * @return
     */
    public static String contractIdAdd(String _cid) {
        try {
            if (_cid.length() != 17) {
                throw new Exception("上一个合同编号非法");
            }
            //先截取后三位
            String _lastNum = _cid.substring(14, 17);
            //获取到的编号中的时间
            String _numDate = _cid.substring(8, 14);
            //当前时间
            String _nowDate = new SimpleDateFormat("yyMMdd").format(new Date());

            if (_numDate.equals(_nowDate)) {
                //如果两个时间相等
                String _tmp = "";
                int _reInt = Integer.parseInt(_lastNum);
                _reInt++;
                _tmp = new DecimalFormat("000").format(_reInt);
                String _preStr = String.format(_cid.substring(0, 14) + "%s", _tmp);

                return _preStr;
            } else {
                String _nowNum = new SimpleDateFormat("yy-MM-dd").format(new Date());
                String[] _nowNumArgs = _nowNum.split("-");

                String _preStr = String.format("ZJBSHT01%s%s%s", _nowNumArgs[0], _nowNumArgs[1], _nowNumArgs[2]);

                _preStr += "001";
                return _preStr;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            log.info("无法获取上一个合同编号:-100");
            return "-100";
        }
    }


    //格式化日期 yyyy-MM-dd HH:mm:ss 转 yyyy-MM-dd
    public static String dateTimeToDate(String _date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            if (!_date.equals("") || _date!=null) {
                Date date = simpleDateFormat.parse(_date);
                String _res = simpleDateFormat.format(date);
                return _res;
            } else {
                return null;

            }
        } catch (Exception e) {
            return simpleDateFormat.format(new Date());
        }
    }

    /**
     * 获取当前时间 yyyy-MM-dd HH:mm:ss
     */
    public static String getDateNow() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String _res = sdf.format(new Date());
        return _res;
    }

    /**
     * 获取当前时间 yyyy-MM-dd
     * @return
     */
    public static String getDateNowYMD() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String _res = sdf.format(new Date());
        return _res;
    }

    /**
     * 转换日期为格林威治时间
     * @param _s
     * @return
     */
    public static String convertGMTDateTime(String _s) {
        if (_s == null) {
            return null;
        }

        SimpleDateFormat s_sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date nowDate = null;
        try {
            nowDate = s_sdf.parse(_s);

        } catch (ParseException pe) {
            //入参的格式不对,在转换一次
            if(_s.length()<=11){
                _s=_s+" 00:00:00";
                try {
                    nowDate = s_sdf.parse(_s);
                } catch (ParseException e) {
                    nowDate = new Date();
                }
            }
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowDate);
        calendar.add(Calendar.HOUR, -8);
        return sdf.format(calendar.getTime());
    }

    /**
     * 获取guid信息
     * @return
     */
    public static String newGuid() {
        UUID guid = UUID.randomUUID();
        return guid.toString();

    }

    //获取当前日期yyyy-MM-dd HH:mm:ss:000

    /**
     * 获取当前时间
     * yyyy-MM-dd HH:mm:ss.SSS
     * @return
     */
    public static String getNowDateTime(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String _res = sdf.format(new Date());
        return _res;
    }

    public static int string2Int(String _s) {
        if (_s == null || _s.equals("") || _s.equals("null")) {
            return 0;
        } else {
            return Integer.valueOf(_s);
        }
    }

    public static double string2Double(String _s) {
        try {

            double _d = (double) Math.round(Double.parseDouble(_s) * 100) / 100;
            return _d;
        } catch (Exception ex) {
            return 0.00;
        }
    }

    //传入Object转为double,如果异常返回0.00
    public static double object2Double(Object _s) {
        try {
            //_s转为double
            if (Objects.isNull(_s)) {
                return 0.00;
            }
            double _d = (double) Math.round(Double.parseDouble(_s.toString()) * 100) / 100;
            return _d;
        } catch (Exception ex) {
            return 0.00;
        }
    }

    public static float formatFloat(float _s) {
        try {

            float _d = (float) Math.round(_s * 100) / 100;
            return _d;
        } catch (Exception ex) {
            return 0;
        }
    }

    public static double formatDouble(double _s) {
        try {

            double _d = (double) Math.round(_s * 100) / 100;
            return _d;
        } catch (Exception ex) {
            return 0.00;
        }
    }

    //计算无税价格
    public static double returnHasTax(double _price, double _tax) {
        try {
            double tax = Double.valueOf(_tax) / 100;
            double pirce = Double.valueOf(_price);
            double redouble = pirce / (1 + tax);
            DecimalFormat df = new DecimalFormat("#.000");
            return Double.parseDouble(df.format(redouble));
        } catch (Exception ex) {
            return 0.000;
        }
    }

    /**
     * 编码url
     *
     * @param str
     * @return
     */
    public static String urlEcode(String str) {
        try {
            String returnStr = new URI(null, str, null).toURL().toString();
            return returnStr;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }

    }
}
